package com.knowif.service;

import java.util.ArrayList;
import java.util.List;

import com.knowif.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.knowif.dao.FuzzySearchMapper;
import com.knowif.system.model.RestBean;
import com.knowif.util.commonUtil.StringUtil;

/**
 * @author xingyao
 * @Description: 模糊查询dao服务层
 */
@Service
public class Fuzzy_service {
	
	@Autowired
	private User_service user_service;
	@Autowired
	private Tutor_service tutor_service;
	@Autowired
	private Label_service label_service;
	@Autowired
	private Topic_service topic_service;
	@Autowired
	private UserEducationExperience_service userEducationExperience_service;
	@Autowired
	private FuzzySearchMapper fuzzySearchMapper;
	@Autowired
	private TutorEducationInfo_service tutorEducationInfo_service;
	
	
	
	/**
	 * @author: xingyao
	 * @Description: 模糊查询tutor信息
	 * @param fuzzySearchName
	 * @return
	 */
	public RestBean<?> fuzzySearchTutor(String fuzzyContext) {
		if(StringUtil.isEmpty(fuzzyContext)){
			return RestBean.buildError("请输入查询信息");
		}
		List<FuzzySearch> fuzzySearchs = selectByFuzzyContext(fuzzyContext);
		if (fuzzySearchs == null || fuzzySearchs.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}
		List<Integer> userIds = FuzzySearch.getUserIds(fuzzySearchs);

		// 查询用户信息
		List<TUser> tusers = user_service.selectByUserIdsAndNotDelete(userIds);

		if (tusers == null || tusers.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}
		userIds.clear();
		
		userIds = TUser.getUserIds(tusers);

		// 查询导师信息
		List<TutorInfo> tutorInfos = tutor_service.selectByUserIds(userIds);
		if (tutorInfos == null || tutorInfos.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}

		tutorInfos = TutorInfo.andTUser(tutorInfos, tusers);
		for (TutorInfo tutorInfo : tutorInfos) {
			tutorInfo.setHighlight(fuzzyContext);
		}
		return RestBean.buildSuccess(tutorInfos);
	}




	private List<FuzzySearch> selectByFuzzyContext(String fuzzyContext) {
		if(StringUtil.isEmpty(fuzzyContext)){
			return new ArrayList<FuzzySearch>();
		}
		FuzzySearchExample fuzzySearchExample = new FuzzySearchExample();
		fuzzySearchExample.createCriteria().andSearchContentLike(fuzzyContext);
		List<FuzzySearch> fuzzySearchs = fuzzySearchMapper.selectByExample(fuzzySearchExample);
		return fuzzySearchs;
	}
	
	
	/**
	 * @author: xingyao
	 * @Description: 模糊查询tutor信息
	 * @param fuzzySearchName
	 * @return
	 */
	@Transactional
	public RestBean<?> fuzzySearchTutor(FuzzySearchExample fuzzySearchExample,String fuzzyContext) {
		if(fuzzySearchExample == null){
			return RestBean.buildError("请输入查询信息");
		}
		List<FuzzySearch> fuzzySearchs = fuzzySearchMapper.selectByExample(fuzzySearchExample);
		if (fuzzySearchs == null || fuzzySearchs.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}
		List<Integer> userIds = new ArrayList<Integer>();
		for (FuzzySearch fuzzySearch : fuzzySearchs) {
			userIds.add(fuzzySearch.getUserId());
		}

		List<TUser> tusers = user_service.selectByUserIdsAndNotDelete(userIds);

		if (tusers == null || tusers.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}
		userIds.clear();
		for (TUser user : tusers) {
			userIds.add(user.getId());
		}

		// 查询导师信息
		List<TutorInfo> tutorInfos = tutor_service.selectByUserIds(userIds);
		if (tutorInfos == null || tutorInfos.isEmpty()) {
			return RestBean.buildError("没有对应的导师信息");
		}
		tutorInfos = TutorInfo.andTUser(tutorInfos, tusers);
		
		List<Label> labels = label_service.selectByUserIds(userIds);
		tutorInfos = TutorInfo.andLabels(tutorInfos, labels);

		List<Topic> topics = topic_service.selectTopicByUserIds(userIds);
		tutorInfos = TutorInfo.andTopics(tutorInfos, topics);

		List<Integer> tutorIds = TutorInfo.getTutorIds(tutorInfos);
		List<UserEducationExperience> educationExperiences = userEducationExperience_service.selectByTutorIds(tutorIds);
		tutorInfos = TutorInfo.andUserEducationExperiences(tutorInfos, educationExperiences);

		//教育信息
		List<TutorEducationInfo> educationInfos = tutorEducationInfo_service.selectByTutorIds(tutorIds);
		TutorInfo.andTutorEducationInfos(tutorInfos, educationInfos);

		//设置高亮信息
		FuzzySearch.saveHighlight(fuzzySearchs, tutorInfos);
		return RestBean.buildSuccess(tutorInfos);
	}
}
























